package cn.tedu.ivos.geofence.service.impl;

import cn.tedu.ivos.base.exception.ServiceException;
import cn.tedu.ivos.base.response.StatusCode;
import cn.tedu.ivos.geofence.mapper.GeofenceMapper;
import cn.tedu.ivos.geofence.pojo.dto.GeofenceParam;
import cn.tedu.ivos.geofence.pojo.dto.GeofenceQuery;
import cn.tedu.ivos.geofence.pojo.entity.Geofence;
import cn.tedu.ivos.geofence.pojo.vo.GeofenceVO;
import cn.tedu.ivos.geofence.service.GeofenceService;
import cn.tedu.ivos.vehicle.mapper.VehicleMapper;
import cn.tedu.ivos.vehicle.pojo.dto.VehicleQuery;
import cn.tedu.ivos.vehicle.pojo.entity.Vehicle;
import cn.tedu.ivos.vehicle.pojo.vo.VehicleVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class GeofenceServiceImpl implements GeofenceService {
    @Autowired
    GeofenceMapper geofenceMapper;
    @Autowired
    VehicleMapper vehicleMapper;
    @Override
    public List<GeofenceVO> select(GeofenceQuery geofenceQuery) {
        log.debug("查询电子围栏业务,参数{}",geofenceQuery);
        List<GeofenceVO> list = geofenceMapper.select(geofenceQuery);
        //之前没有对围栏的车辆数据进行处理,现在需要对数据进行补充
        /* 遍历每一个围栏,依次为每个围栏补充车辆数据 totalNum availableNum vehicleList*/
        for (int i = 0;i < list.size();i++){
            GeofenceVO geofenceVO = list.get(i);
            VehicleQuery vehicleQuery = new VehicleQuery();
            vehicleQuery.setGeofenceId(geofenceVO.getId());
            List<VehicleVO> vehicleVOList = vehicleMapper.selectVehicle(vehicleQuery);
            int totalNum = vehicleVOList.size();
            int availableNum = 0;
            for (VehicleVO vehicleVO : vehicleVOList){
                if (vehicleVO.getStatus().equals("1")){
                    availableNum++;
                }
            }
            //8.给围栏对象设置车辆数据
            geofenceVO.setTotalNum(totalNum);
            geofenceVO.setAvailableNum(availableNum);
            //此处的车辆列表是供后续分配车辆时查看车辆详情时使用
            geofenceVO.setVehicleList(vehicleVOList);
        }
        return list;
    }

    @Override
    public void saveGeofence(GeofenceParam geofenceParam) {
        log.debug("更新电子围栏业务,参数{}",geofenceParam);
        Geofence geofence = new Geofence();
        BeanUtils.copyProperties(geofenceParam,geofence);
        if (geofence.getId()!=null){
            geofence.setUpdateTime(new Date());
            geofenceMapper.update(geofence);
        }else {
            geofence.setStatus("1");
            geofence.setCreateTime(new Date());
            geofenceMapper.insert(geofence);
        }
        geofenceMapper.update(geofence);
    }

    @Override
    public void deleteGeofence(Long geofenceId) {
        log.debug("删除电子围栏业务,参数{}",geofenceId);
        //1.封装车辆查询的DTO层对象VehicleQuery
        VehicleQuery vehicleQuery = new VehicleQuery();
        vehicleQuery.setGeofenceId(geofenceId);
        //2.调用车辆数据层查询是否有绑定在当前围栏上的车辆
        List<VehicleVO> list = vehicleMapper.selectVehicle(vehicleQuery);
        //3.如果当前围栏上有绑定车辆,业务层可以抛出操作失败的异常
        if (list.size()>0 && list!=null) {
            throw new ServiceException(StatusCode.OPERATION_FAILED);
        }else {
            //4.如果该围栏下没有绑定车辆,可以正常删除
            geofenceMapper.delete(geofenceId);
        }

    }
}
